// pages/order/order.js
var app = getApp()
const $api = require('../../utils/api').API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件所需的参数
    navbarData: {
      havebg: 1, //1表示不显示    0表示显示
      out: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '全部订单', //导航栏 中间的标题
      bg: '#fff', //导航背景
    },
    height: app.globalData.height,
    loadModal: true,
    page: 1,
    bottom: true,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    //4=全部 
    if (options.type == 4) {
      var status = '';
    } else {
      var status = options.type
    }
    var _this = this;
    _this.setData({
      status: status
    })
  },
  onShow() {
    var _this = this;
    var status = _this.data.status;
    $api.getOrderLists({
      login_token: wx.getStorageSync('login_token'),
      page: 1,
      status: status,
      is_shop_order: 0
    }).then((res) => {
      _this.setData({
        list: res.data.data,
        loadModal: false,
        page: 1,
        bottom: true
      })
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })
  },
  //顶部导航
  onTopNav(e) {
    console.log(e)
    var _this = this;
    _this.setData({
      loadModal: true,
      bottom: true,
      page: 1
    })
    var status = e.currentTarget.dataset.type;
    $api.getOrderLists({
      login_token: wx.getStorageSync('login_token'),
      page: 1,
      status: status,
      is_shop_order: 0
    }).then((res) => {
      _this.setData({
        list: res.data.data,
        loadModal: false,
        status: status
      })
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })


  },
  //加载
  onReachBottom() {
    var _this = this;
    var page = _this.data.page;
    var bottom = _this.data.bottom;
    var status = _this.data.status;
    if (bottom) {
      _this.setData({
        loadModal: true
      })
      page++;
      $api.getOrderLists({
        login_token: wx.getStorageSync('login_token'),
        page: page,
        status: status,
        is_shop_order: 0
      }).then((res) => {
        if (res.data.data.data.length <= 0) {
          _this.setData({
            bottom: false,
            page: page
          })
          wx.showToast({
            title: '到底了',
            icon: 'none',
            duration: 2000
          })
        } else {
          _this.setData({
            ['list.data']: _this.data.list.data.concat(res.data.data.data),
            page: page
          })
        }
        _this.setData({
          loadModal: false,
        })
      }).catch((err) => {
        _this.setData({
          loadModal: false
        })
      })
    }


  },
  //取消订单
  onCancel(e) {
    var _this = this;
    _this.setData({
      loadModal: true
    })
    $api.delOrder({
      login_token: wx.getStorageSync('login_token'),
      order_id: e.currentTarget.dataset.id
    }).then((res) => {
      _this.setData({
        loadModal: false
      })
      wx.showToast({
        title: '取消成功',
        icon: 'none',
        duration: 2000
      })
      setTimeout(function () {
        _this.onShow()
      }, 1500)
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })
  },
  //修改地址
  onModify(e) {
    wx.navigateTo({
      url: '/pages/address/list/list?choice=1&order=1&orderid=' + e.currentTarget.dataset.orderid,
    })
    // wx.navigateTo({
    //   url: '/pages/address/modify/modify?order=1&orderid=' + e.currentTarget.dataset.orderid,
    // })
  },
  //支付
  onPay(e) {
    var _this = this;
    _this.setData({
      loadModal: true
    })
    wx.navigateTo({
      url: '/pages/Payment/Payment?upType=order&order_id=' + e.currentTarget.dataset.id,
    })
    _this.setData({
      loadModal: false
    })
    return
    //支付
    wx.login({
      success(res) {
        console.log(res)
        $api.getOpenidAppPay({
          js_code: res.code
        }).then((res) => {
          console.log(res.data, 66)
          if (res.data.code == 0) {
            var openid = res.data.data.openid;
            $api.appPayFunc({
              login_token: wx.getStorageSync('login_token'),//用户登录token
              order_id: e.currentTarget.dataset.id,//订单id
              pay_type: 4,//支付类型 4 第三方微信支付 
              is_fp: 0,//是否分批付款 0否 1是
              // fp_money: options.fp_money,//付款金额
              sf_openid: openid,//三方支付微信支付的openid

            }).then((res) => {
              if (res.data.code == 0) {
                var payData = JSON.parse(res.data.data.sfwxdata.expend.pay_info);
                var packages = payData.package;
                var timeStamp = payData.timeStamp;
                wx.requestPayment({
                  timeStamp: timeStamp.toString(),
                  nonceStr: payData.nonceStr,
                  package: packages,
                  signType: payData.signType,
                  paySign: payData.paySign,
                  success(res) {
                    console.log(res)
                    //支付成功
                    _this.onShow()
                  },
                  fail(res) {
                    console.log(res)
                    _this.setData({
                      loadModal: false
                    })
                  },
                })

              } else {
                wx.showToast({
                  title: res.data.message,
                  icon: 'none',
                  duration: 2000
                })
              }
            })
          }
        })
      }
    })
    // $api.wxPay({
    //   login_token: wx.getStorageSync('login_token'),
    //   order_id: e.currentTarget.dataset.id,
    // }).then((res) => {
    //   var wxPay = res.data.data;
    //   var timeStamp = wxPay.timeStamp
    //   wx.requestPayment({
    //     timeStamp: timeStamp.toString(),
    //     nonceStr: wxPay.nonceStr,
    //     package: wxPay.package,
    //     signType: wxPay.signType,
    //     paySign: wxPay.paySign,
    //     success(res) {
    //       console.log(res)
    //       _this.onShow()
    //     },
    //     fail(res) {
    //       console.log(res)
    //       _this.setData({
    //         loadModal: false
    //       })
    //     }
    //   })
    // }).catch((err) => {
    //   _this.setData({
    //     loadModal: false
    //   })
    // })
  },
  //提醒发货
  onRemind() {
    var _this = this;
    _this.setData({
      loadModal: true
    })
    setTimeout(function () {
      _this.setData({
        loadModal: false
      })
      wx.showToast({
        title: '已提醒商家，商家会尽快安排发货',
        icon: 'none',
        duration: 2000
      })
    }, 500)

  },
  //申请退款
  onRefund(e) {
    var _this = this;
    var id = e.currentTarget.dataset.id;
    var list = _this.data.list.data;
    var before = e.currentTarget.dataset.before; //1==发货前  2==发货后
    var order;
    for (var i = 0; i < list.length; i++) {
      if (list[i].id == id) {
        order = list[i]
      }
    }
    console.log(order)
    if (order.detail.length == 1) {
      _this.setData({
        loadModal: true
      })
      $api.selectOrdersGoods({
        login_token: wx.getStorageSync('login_token'),
        order_id: id
      }).then((res) => {
        let goods = []
        res.data.data.forEach((item) => {
          if (item.asale_status == 0) {
            goods.push(item)
          }
        })
        if (goods.length == 0) {
          wx.showToast({
            title: "无售后商品",
            icon: 'none',
            duration: 2000
          })
          return
        }
        res.data.data[0].icon = false
        wx.setStorageSync('goods', goods);
        let fee = res.data.fee
        var price = res.data.data[0].price * res.data.data[0].num
        wx.setStorageSync('price', price);
        wx.setStorageSync('fee', fee);
        wx.setStorageSync('goodslistlength', '');
        _this.setData({
          loadModal: false
        })

      }).catch((err) => {
        _this.setData({
          loadModal: false
        })
      })
      //发货前
      if (before == 1) {
        wx.navigateTo({
          url: '/pages/sale/refund/refund',
        })

      }
      //发货后
      if (before == 2) {
        wx.navigateTo({
          url: '/pages/sale/sale/sale',
        })

      }



    }
    if (order.detail.length > 1) {
      //发货前
      if (before == 1) {
        wx.navigateTo({
          url: '/pages/sale/detail/detail?id=' + id + '&price=' + e.currentTarget.dataset.price + '&before=' + before,
        })

      }
      //发货后
      if (before == 2) {
        $api.selectOrdersGoods({
          login_token: wx.getStorageSync('login_token'),
          order_id: id
        }).then((res) => {
          let goods = []
          res.data.data.forEach((item) => {
            if (item.asale_status == 0) {
              goods.push(item)
            }
          })
          if (goods.length == 0) {
            wx.showToast({
              title: "无售后商品",
              icon: 'none',
              duration: 2000
            })
            return
          }
          wx.setStorageSync('goods', goods);
          let fee = res.data.fee
          // wx.setStorageSync('price', price);
          wx.setStorageSync('fee', fee);
          wx.setStorageSync('goodslistlength', '');
          wx.navigateTo({
            url: '/pages/sale/sale/sale',
          })
          _this.setData({
            loadModal: false
          })
  
        }).catch((err) => {
          _this.setData({
            loadModal: false
          })
        })
        

      }
      

    }



  },
  //确认收货
  onReceiving(e) {
    var _this = this;
    _this.setData({
      loadModal: true
    })
    $api.confirmDelivery({
      login_token: wx.getStorageSync('login_token'),
      order_id: e.currentTarget.dataset.id,
    }).then((res) => {
      wx.showToast({
        title: "确认收货完成",
        icon: 'none',
        duration: 2000
      })
      _this.setData({
        loadModal: false
      })
      setTimeout(function () {
        _this.onShow()
      }, 1500)
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })
  },
  //进入订单详情
  onOrderDetails(e) {
    wx.navigateTo({
      url: '/pages/orderDetails/orderDetails?orderid=' + e.currentTarget.dataset.orderid,
    })
  },
  //下拉刷新
  onPullDownRefresh() {
    var _this = this;
    _this.setData({
      loadModal: true
    })
    setTimeout(function () {
      _this.onShow();
      wx.stopPullDownRefresh()
    }, 1000)



  },








})